/**
 ****************************************************************************
 * Copyright (C) Marcelo F. Ochoa. All rights reserved.                      *
 * ------------------------------------------------------------------------- *
 * This software is published under the terms of the Apache Software License *
 * version 1.1, a copy of which has been included  with this distribution in *
 * the LICENSE file.                                                         *
 */

package com.prism.toolkit;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.sql.SQLException;

/* Wrapper class for image form data type.
 * used when declare <input type=image name=i_button>
 * Procedures which handle this argument must be declared as
 * public void proc(byte []i_button,....)
 * then this argument could by converted from byte[] to JowaImage
 * by using the provides constructor.
 */

public class JowaImage implements Serializable {
    int x;
    int y;

    public JowaImage() {
    }

    public JowaImage(byte[] st) throws SQLException {
        try {
            ByteArrayInputStream i = new ByteArrayInputStream(st);
            ObjectInputStream in = new ObjectInputStream(i);
            JowaImage pt = (JowaImage)in.readObject();
            x = pt.x;
            y = pt.y;
        } catch (Exception e) {
            throw new SQLException(e.getMessage());
        }
    }

    public byte[] getBytes() throws SQLException {
        try {
            ByteArrayOutputStream o = new ByteArrayOutputStream();
            ObjectOutputStream out = new ObjectOutputStream(o);
            out.writeObject(this);
            out.flush();
            return o.toByteArray();
        } catch (Exception e) {
            throw new SQLException(e.getMessage());
        }
    }

    public JowaImage(int px, int py) {
        x = px;
        y = py;
    }

    public JowaImage(String str) {
        try {
            x = (new Integer(str.substring(0, str.indexOf(',')))).intValue();
        } catch (Exception e) { x = 0; };
        try {
            y = (new Integer(str.substring(str.indexOf(',') + 1))).intValue();
        } catch (Exception e) { y = 0; };
    }

    public static void main(String[] args) {
        new JowaImage();
        try {
            JowaImage p1 = new JowaImage(20, 35);
            JowaImage p2 = new JowaImage(13, 7);
            ByteArrayOutputStream o = new ByteArrayOutputStream();
            ObjectOutputStream out = new ObjectOutputStream(o);
            out.writeObject(p1);
            out.writeObject(p2);
            out.flush();
            ByteArrayInputStream i = new ByteArrayInputStream(o.toByteArray());
            ObjectInputStream in = new ObjectInputStream(i);
            JowaImage p3 = new JowaImage();
            JowaImage p4 = new JowaImage();
            p3 = (JowaImage)in.readObject();
            p4 = (JowaImage)in.readObject();
            System.out.println("p3.x= " + p3.getX() + " p3.y= " + p3.getY());
            System.out.println("p4.x= " + p4.getX() + " p4.y= " + p4.getY());
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    public String toString() {
        return x + "," + y;
    }

    public int getX() {
        return x;
    }

    public int getY() {
        return y;
    }

    public void setX(int px) {
        x = px;
    }

    public void setY(int py) {
        y = py;
    }

    public void setX(String sx) {
        try {
            x = (new Integer(sx)).intValue();
        } catch (Exception e) { x = 0; };
    }

    public void setY(String sy) {
        try {
            y = (new Integer(sy)).intValue();
        } catch (Exception e) { y = 0; };
    }
}
